<template>
  <div class="detalis">
    <!-- 详情页 -->

    <!-- 详情 -->
    <div class="container">
      <!-- 面包屑导航 -->
      <div class="top">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item 
            v-for="item in arrs"
            :key="item.id"
          >{{item.name}}
          </el-breadcrumb-item>
          <el-breadcrumb-item>{{msgs.name}}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 主体内容 -->
      <div class="box container">
        <div class="lef">
          <!-- 放大镜 -->
          <magnifier v-if="pic.length" :pict="pic" :properties="properties"></magnifier>
        </div>
        <div class="rig">
          <h3 class="tit1">{{msgs.name}}</h3>
          <p class="tit2">{{msgs.desc}}</p>
          <p class="price">
            <!-- 当前价格 -->
            <span class="red"><span>￥</span>{{msgs.price}}</span>
            <!-- 原价 -->
            <span class="cc"><span>￥</span>{{msgs.oldPrice}}</span>
          </p>
          <div class="bg">
            <p>促销
              <span>12月好物放送，App领券购买直降120元</span>
            </p>
            <p>配送 至
              <el-cascader
                size="large"
                :options="options"
                v-model="selectedOptions"
                @change="handleChange">
              </el-cascader>
            </p>
            <p>服务
              <span>无忧退换</span>
              <span>快速退款</span>
              <span>免费包邮</span>
            </p>
          </div>
          <!-- 参数-颜色 -->
          <div class="cs">
            <div class="box1">
              <span>{{color.name}}</span>
              <img 
                v-for="(item,index) in color.values" 
                :key="item.picture"
                :src="item.picture" 
                alt=""
                :class="['image',{oncolor:colorIndex == index}]"
                @click="onActive(index,item.name)"
              >
            </div>
            <div class="box2">
              <div class="name1">{{spec.name}}</div>
              <div 
                :class="['border',{oncolor:activeName == index}]"
                v-for="(item,index) in spec.values"
                :key="item.name"
                @click="onName(index,item.name)"
              >{{item.name}}</div>
            </div>
            
          </div>
          <!-- 数量 -->
          <div class="num">
            <span>数量</span>
            <el-input-number v-model="num" @change="handleChange2" :min="1" :max="99" label="描述文字"></el-input-number>
          </div>
          <!-- 加入购物车 -->
          <div class="btn">
            <el-button @click="addCart" type="success">加入购物车</el-button>
            
          </div>
        </div>
      </div>
        <!-- 同类推荐 -->
        <div class="recom"></div>
        <!-- 商品详情 -->
        <div class="date">
          <botImg  :botImg="botImg"></botImg>
        </div>
    </div>
    
  </div>
</template>

<script>
import axios from 'axios';
import magnifier from './components/magnifier.vue';
import botImg from './components/bottomImg.vue'
import { provinceAndCityData } from 'element-china-area-data'; //选择地区
// import {insertCart} from '@/api/cart'
export default {
  name: "DetailsView",
  data() {
    return {
      msgs:{}, //总数居
      arrs:[], //面包屑导航
      pic:[], //放大镜图片
      properties:[], //品牌信息
      options: provinceAndCityData,
      selectedOptions: [],
      color:[], //颜色
      spec:[], //规格
      num: 1, //数量
      id:'', //id
      colorIndex:-1, //是否选中的颜色
      activeName:-1, //是否选中规格
      names1:'', //选中的颜色
      names2:'', //选中的规格
      price:0, //金额
      botImg:[], //底部图片简介
    };
  },
  components:{
    magnifier,
    botImg,
  },
  methods:{
    // 成功
    open2() {
        this.$message({
          message: '恭喜你，加入购物车成功',
          type: 'success'
        });
      },
      // 警告
      open3() {
        this.$message({
          message: '警告，请选择规格',
          type: 'warning'
        });
      },
    // 加入购物车
    addCart(){
      console.log('加入购物车');

      let obj1 = this.names1;
      let obj2 = this.names2;
      console.log(this.names1);
      if(this.names1 && this.names2){
        // console.log(222);
        for (let index = 0; index < this.msgs.skus.length; index++) {
          const elem = this.msgs.skus[index];
          // console.log(elem);
          for(let j = 0;j < elem.specs.length;j++){
            // console.log(elem.specs[j]);
            if(elem.specs[j].valueName == obj1 || elem.specs[j].valueName == obj2){
              if(elem.specs[j].valueName == obj1 || elem.specs[j].valueName == obj2){
                this.id = elem.skuCode;
                // this.price = elem.price
              }
            }
          }
        }
      }

      if(this.id){
        let obj = {
          skuId:this.id,
          count:this.num
        }
        console.log(obj);
        // insertCart(obj).then( res => {
        //   console.log(res);
        // })
        axios.post('/member/cart',obj).then(res => {
          console.log(res);
          if(res.status == 200){
            this.open2()
          }else{
            this.open3()
          }
        })
      }
      
      
    },


    // 选中颜色
    onActive(index,name){
      index == this.colorIndex ? this.colorIndex = -1 : this.colorIndex = index;
      this.names1 = name
      
    },
    // 选中规格
    onName(index,name){
      index == this.activeName ? this.activeName = -1 : this.activeName = index;
      this.names2 = name
      
    },
    // 地区
    handleChange (value) {
        console.log(value)
    },
    // 数量
    handleChange2(value) {
        console.log(value);
    },
    init(id=3994893){
      let arr = [];
      axios.get('/goods?id='+id).then(res => {
        //        https://apipc-xiaotuxian-front.itheima.net/goods?id=3994487
        console.log(res.data.result);
        
          
        // this.id = res.data.result.id;
        
        // this.head = res.data.result;
        this.msgs=res.data.result
        console.log('颜色和规格 =>>' + res.data.result.specs);
        res.data.result.categories.forEach(element => {
          arr.unshift(element)
        });
        console.log('面包屑导航===>'+arr);
        this.arrs = arr;
        // 底部图片简介
        // if(res.data.result.details.length){
          this.botImg = res.data.result.details.pictures;
          console.log(this.botImg);
        // }
        // 放大镜图片
        if(res.data.result.mainPictures.length){
          this.pic = res.data.result.mainPictures
        }
        // console.log(res.data.result.details.properties);
        // 品牌信息
        if(res.data.result.details.properties.length){
          this.properties = res.data.result.details.properties
        }
        if(res.data.result.specs.length){
          // 颜色
          this.color = res.data.result.specs[0];
          // 规格
          this.spec = res.data.result.specs[1]
        }
      })
    }
  },
  computed: {
    categoryPath() {
      return "/home/category?" + this.msgs.categoryies[0].id;
    },
    subPath() {
      return "/home/category/sub?" + this.msgs.categoryies[1].id;
    },
    categoryName() {
      return "/home/category?" + this.msgs.categoryies[0].name;
    },
    subName() {
      return "/home/category/sub?" + this.msgs.categoryies[1].name;
    },
  },
  mounted() {
    this.init(this.$route.query.id)
  },
};
</script>

<style scoped lang="less">
.detalis {
  background-color: #f5f5f5;
  .date{
    width: 100%;
    // background-color: red;
  }
  .top {
    padding: 20px 0;
  }
  .box{
    background-color: #fff;
    min-height: 600px;
    box-sizing: border-box;
    padding: 20px 40px;
    display: flex;
    .lef{
      width: 520px;
      min-height: 500px;
      position: relative;
    }
    .rig{
      width: 660px;
      min-height: 500px;
      background-color: rgb(255, 255, 255);
      .tit1{
        font-size: 26px;
      }
      .tit2{
        margin-top: 6px;
        margin-bottom: 6px;
        font-size: 16px;
        color: #ccc;
      }
      .red{
        font-size: 24px;
        color: red;
        margin-right: 10px;
        span{
          font-size: 14px;
        }
      }
      .cc{
        font-size: 16px;
        color: #ccc;
        text-decoration:line-through
      }
      .bg{
        width: 100%;
        height: 100px;
        background-color: #f5f5f5;
        padding: 10px 0 0 10px;
        margin: 10px 0;
        color: #999;
        p{
          margin-bottom: 10px;
          span{
            color: #666;
            margin: 0 8px;
          }
        }
      }
      .num{
        margin: 10px 0;
      }
      .cs{
        .image{
          width: 60px;
          height: 60px;
          margin-right: 10px;
          border: solid 1px #ccc;
        }
        .oncolor{
          border: solid 1px rgb(103, 241, 103);
        }
        .box2{
          display: flex;
          margin-top: 10px;
          
          .border{
            padding: 5px 10px;
            margin-right: 10px;
            border: solid 1px #ccc;
          }
          .oncolor{
            border: solid 1px rgb(103, 241, 103);
          }
        }
        
      }
    }
  }
}
</style>